<?php

namespace Modules\Stats;

use Modules\Stats\Entities\PlatformStats;

class Cron
{
	public function handle($schedule)
	{
		$that = $this;
		$schedule->call(function() use ($that) {
			try {
				$that->everyMinute();
			}
			catch (\Throwable $e) {
                \Log::error('stats cron任务异常');
                \Log::error($e->getMessage());
            }
		})->everyMinute();

		$schedule->call(function() use ($that) {
			try {
				$that->everyHour();
			}
			catch (\Throwable $e) {
                \Log::error('stats cron任务异常');
                \Log::error($e->getMessage());
            }
		})->hourly();

		$schedule->call(function() use ($that) {
			try {
				$that->everyDay();
			}
			catch (\Throwable $e) {
                \Log::error('stats cron任务异常');
                \Log::error($e->getMessage());
            }
		})->daily();
	}

	public function everyMinute()
	{
	}

	public function everyDay()
	{
		// 更新昨天的指标
		$day = date('Y-m-d', strtotime('-1 hours'));
		PlatformStats::updateAtDayEnd($day);
		PlatformStats::updateAll($day);
		\Log::error('更新每日统计:' . $day);
	}

	public function everyHour()
	{
		$day = date('Y-m-d');
		// 更新当天的指标
		PlatformStats::updateAtDayEnd($day);
		PlatformStats::updateAll($day);
	}
}